package com.stark.wechat.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ComponentScan.Filter;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Description;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.ImportResource;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.transaction.support.TransactionTemplate;
import org.springframework.web.bind.annotation.ControllerAdvice;

import com.stark.wechat.context.MultipleDataSource;

/**
 * Spring 配置。
 * @author mengbin
 * @since 1.1.0
 * @version 1.0.0, 2017/9/22
 */
@Configuration
@ComponentScan(basePackages = "com.stark.wechat",
	excludeFilters = @Filter({ Configuration.class, Controller.class, ControllerAdvice.class }))
@EnableAspectJAutoProxy
@EnableTransactionManagement
@ImportResource("classpath:applicationContext.xml")
@Import(MyBatisConfig.class)
public class SpringConfig {
	@Bean
	public JdbcTemplate jdbcTemplate(MultipleDataSource multipleDataSource) {
		return new JdbcTemplate(multipleDataSource);
	}
	
	@Bean
	@Description("事务管理器")
	public PlatformTransactionManager transactionManager(MultipleDataSource multipleDataSource) {
		return new DataSourceTransactionManager(multipleDataSource);
	}
	
	@Bean
	@Description("事务模板")
	public TransactionTemplate transactionTemplate(MultipleDataSource multipleDataSource) {
		TransactionTemplate transactionTemplate = new TransactionTemplate(transactionManager(multipleDataSource));
		transactionTemplate.setPropagationBehavior(TransactionTemplate.PROPAGATION_REQUIRED);
		return transactionTemplate;
	}
}
